import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

//createRouter 创建路由实例
//配置history 模式
//1.history 模式：createWebHistory  地址栏不带#
//2.hash 模式：createWebHashHistory 地址栏带#

//vite中的base

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') }, //登录页
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContsiner.vue'), //首页
      redirect: '/atricle/Control',
      children: [
        {
          path: '/atricle/Control',
          component: () => import('@/views/atricle/Index.vue')
        },
        {
          path: '/atricle/Manage',
          component: () => import('@/views/atricle/MatronPage.vue')
        },
        {
          path: '/atricle/Comment',
          component: () => import('@/views/atricle/CommentPage.vue')
        },
        {
          path: '/atricle/Advertising',
          component: () => import('@/views/atricle/advertisingPage.vue')
        },
        {
          path: '/atricle/Order',
          component: () => import('@/views/atricle/OrderPage.vue')
        },
        {
          path: '/atricle/Community',
          component: () => import('@/views/atricle/communityManage.vue')
        }
      ]
    },
    {
      path:'/index.html',
      component: () => import('@/views/layout/LayoutContsiner.vue'), //首页
      redirect:'/',
     
    }

  ]
})

// 登录访问拦截
// router.beforeEach((to) => {
//   const userStore = useUserStore()
//   if (!userStore.token && to.path !== '/login') return '/login'
// })


const whiteList=['/login','/404']
router.beforeEach((to , from ,next)=>{
  const userstore=useUserStore()
nprogress.start()
// console.log('dsa',userstore.token)
if(userstore.token){
//存在token
if(to.path==='/login'){
    nprogress.done()
    next('/')
}
else {
    // console.log('存在tkoen')
    next()
}
}
else {
//没有token
if (whiteList.includes(to.path)){
next()
}
else {
next('/login')
}
}

})

router.afterEach((to,from,next)=>{
nprogress.done()
})

export default router
